import { applyMiddleware, legacy_createStore as createStore, combineReducers } from 'redux';
import middlewares from './middlewares';


const dynamicReducers = {};
const store = createStore(
  combineReducers({}),
  applyMiddleware(...middlewares),
);

const addReducer = (reducer = {}) => {
  if (Object.prototype.toString.call(reducer) !== '[object Object]') {
    throw new Error(`reducer must be a object.`);
  }

  Object.keys(reducer).forEach((key) => {
    if (dynamicReducers[key]) {
      if (process.env.NODE_ENV !== 'production') {
        console.warn(`reducer ${key} is existed.`);
      }
      return false;
      // throw new Error(`reducer ${key} is existed.`);
    }
  });

  Object.assign(dynamicReducers, reducer);

  store.replaceReducer(
    combineReducers({
      ...dynamicReducers,
    }),
  );
};
export { addReducer };
export default store;
